package com.example.homework7;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;



import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

class Sha256Util{

    /**
     * 利用java原生的类实现SHA256加密
     * @param str 加密后的报文
     * @return
     */
    public static String getSHA256(String str){
        MessageDigest messageDigest;
        String encodestr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodestr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodestr;
    }
    /**
     * 将byte转为16进制
     * @param bytes
     * @return
     */
    private static String byte2Hex(byte[] bytes){
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i=0;i<bytes.length;i++){
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length()==1){
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

}



public class SharedPreferences extends Fragment {
    public SharedPreferences(){super(R.layout.sp);}

    private Button button7;
    private Button button5;
    private EditText editTextTextPersonName;
    private EditText editTextTextPersonName2;
    private EditText editTextTextPersonName3;
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        Button button7=(Button) getActivity().findViewById(R.id.button7);
        Button button5=(Button) getActivity().findViewById(R.id.button5);
        EditText editTextTextPersonName=(EditText) getActivity().findViewById(R.id.editTextTextPersonName);
        EditText editTextTextPersonName2=(EditText) getActivity().findViewById(R.id.editTextTextPersonName2);
        EditText editTextTextPersonName3=(EditText) getActivity().findViewById(R.id.editTextTextPersonName3);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                android.content.SharedPreferences settings=  getActivity().getSharedPreferences("sp.txt",Context.MODE_PRIVATE);
                android.content.SharedPreferences.Editor editor=settings.edit();



                editor.putString("name",editTextTextPersonName.getText().toString());
                editor.putString("num",Sha256Util.getSHA256(editTextTextPersonName2.getText().toString()));
                editor.putString("class",editTextTextPersonName3.getText().toString());
                editor.commit();
                Toast.makeText(getActivity(),"保存成功",Toast.LENGTH_SHORT).show();
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                android.content.SharedPreferences settings=getActivity().getSharedPreferences("sp.txt",Context.MODE_PRIVATE);
                String name=settings.getString("name","");
                String num=settings.getString("num","");
                String className=settings.getString("class","");
                editTextTextPersonName.setText(name);
                editTextTextPersonName2.setText(num);
                editTextTextPersonName3.setText(className);

            }
        });


    }


}
